#coding=utf-8
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Eday(models.Model):
    '''交易日'''
    date = models.DateField()
    pre = models.ForeignKey('self', unique=True, null=True, blank=True)
   
    content_type = models.ForeignKey(ContentType)
    object_id = models.IntegerField()
   
    finance_tool = generic.GenericForeignKey()
    
    class Meta:
        app_label = 'data'
        
    def __unicode__(self):
        return self.date.isoformat()
    
    def get_nextday(self):
        a = self.eday_set.all()
        if a:
            return a[0]
        return None
    next = property(fget=get_nextday)
    
    def get_ts(self):
        f = self.finance_tool
        if not getattr(f, 'ts', None):
            return None
        return f.tradableshare_set.order_by('-since').filter(since__lte=self.date)[0].quantity
    ts = property(fget=get_ts)
